package com.tuniu.agents.tools.trip.tool;

import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.google.gson.annotations.SerializedName;
import com.tuniu.agents.tools.trip.TripProperties;
import com.tuniu.agents.util.HttpManager;
import com.tuniu.agents.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.BiFunction;

public class ItineraryPlanListTool implements BiFunction<ItineraryPlanListTool.Request, ToolContext, ItineraryPlanListTool.Response> {

    private static final Logger logger = LoggerFactory.getLogger(ItineraryPlanListTool.class);

    public static final String TOOLNAME = "itinerary_plan_list";

    private final String tripDesignerUrl;

    @Autowired
    private HttpManager httpManager;

    public ItineraryPlanListTool(TripProperties properties) {
        this.tripDesignerUrl = properties.getTripDesignerUrl();
    }

    @Override
    public Response apply(Request request, ToolContext toolContext) {
        try {

            logger.info("ItineraryPlanListTool apply request:{}", JsonUtil.toJson(request));
            Request newRequest = new Request(request.queryText(),5);
            ItineraryPlanListTool.Response response = httpManager.post(tripDesignerUrl, newRequest, ItineraryPlanListTool.Response.class);
            if(!response.code().equals(200) || CollectionUtils.isEmpty(response.data())){
                logger.warn("ItineraryPlanListTool apply fail or empty request:{}", JsonUtil.toJson(newRequest));
                return null;
            }
            return response;
        }catch (Exception e){
            logger.error("ItineraryPlanListTool apply error request:" + JsonUtil.toJson(request),e);
            return null;
        }
    }

    @JsonClassDescription("BI obtains itinerary information")
    public record Request(
            @SerializedName(value = "query_text") @JsonProperty(value = "query_text") @JsonPropertyDescription("search text") String  queryText,
            @SerializedName(value = "top_k") @JsonProperty(value = "top_k", defaultValue = "5") @JsonPropertyDescription("The first few are 5 by default") Integer topK
    ){}

    @JsonClassDescription("BI itinerary data return")
    public record Response(
            @JsonProperty(value = "code") @JsonPropertyDescription("status code")  Integer code,
            @JsonProperty(value = "msg") @JsonPropertyDescription("status information")  String msg,
            @JsonProperty(value = "data") @JsonPropertyDescription("data") List<Itinerary> data
    ){}

    @JsonClassDescription("product information")
    public record Product(
          @JsonProperty(value = "name") @JsonPropertyDescription("product name")  String name,
          @JsonProperty(value = "url") @JsonPropertyDescription("product link")  String url
    ){}

    @JsonClassDescription("resource information")
    public record Resource(
          @JsonProperty(value = "product") @JsonPropertyDescription("product information")  Product[] product
    ){}

    @JsonClassDescription("itinerary information")
    public record Itinerary(
          @SerializedName(value = "product_id")  @JsonProperty(value = "product_id") @JsonPropertyDescription("product id")  String productId,
          @SerializedName(value = "similarity_score") @JsonProperty(value = "similarity_score") @JsonPropertyDescription("similarity score")  Double similarityScore,
          @SerializedName(value = "itinerary_plan") @JsonProperty(value = "itinerary_plan") @JsonPropertyDescription("itinerary plan")  String itineraryPlan,
          @SerializedName(value = "itinerary_scenic_spots") @JsonProperty(value = "itinerary_scenic_spots") @JsonPropertyDescription("itinerary scenic spots")  String itineraryScenicSpots,
          @SerializedName(value = "itinerary_hotels") @JsonProperty(value = "itinerary_hotels") @JsonPropertyDescription("itinerary hotels")  String itineraryHotels,
          @JsonProperty(value = "resource") @JsonPropertyDescription("resource")  Resource resource
    ){}

}
